{% extends "../main.html" %}

{% block title %}{{ _("Home") }}{% end %}

{% block header %}
{% from models.Category import Category %}
<script>
    var categories = '{{ Category.list() }}'.replaceAll('&quot;','"');
</script>
<script src="/static/js/libs/highcharts-all.js"></script>
<script src="/static/js/libs/highcharts-more.js"></script>
<script src="/static/js/pages/missions/view.js"></script>
<script src="/static/js/pages/user/home.js"></script>
<link href="/static/css/pages/user/home.css" rel="stylesheet" />
{% end %}

{% block content %}
{% from models.GameLevel import GameLevel %}

{% from models.Hint import Hint %}
{% from tornado.options import options %}
{% set team = user.team %}
<div class="row-fluid">
    <div class="span6">
        <div class="row">
            <div class="span11 offset1 well">
                <h2 style="padding-bottom: 8px;">
                    <i class="fa fa-user"></i> {{ user.handle }} 
                {% if options.teams %}<span style="float: right; font-size: 60%; margin-top: 8px;">{% if options.banking %}${% end %}{{ user.money }} &bull; {{ len(user.flags) }} <i class="fa fa-flag-o"></i></span></h2>{% end %}
                {% if options.teams %}<span style="float: right; margin-top: -63px;">{{ _("Player Score") }}</span>{% end %}
                <center>
                    {% if options.teams or not team %}
                        <img src="/avatars/{{ user.avatar }}" class="img-polaroid" />
                    {% else %}
                        <img src="/avatars/{{ team.avatar }}" class="img-polaroid" />
                    {% end %}
                </center>
                <hr>
                {% if team is not None %}
                    {% set hintcount = len(Hint.all()) %}
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th><h5 style="display: inline;">{% if options.teams %}{{ _("Team") }}{% else %}{{ _("Player") }}{% end %}</h5></th>
                                <th style="text-align: center;"><h5 style="display: inline;">{{ _("Current Level") }}</h5></th>
                                {% if options.use_bots %}
                                    <th style="text-align: center;"><h5 style="display: inline;">{{ _("Active Bots") }}</h5></th>
                                {% end %}
                                {% if hintcount > 0 %}
                                <th style="text-align: center;"><h5 style="display: inline;">{{ _("Hints Taken") }}</h5></th>
                                {% end %}
                                <th style="text-align: center;"><h5 style="display: inline;">{{ _("Flags Captured") }}</h5></th>
                                <th style="text-align: right;"><h5 style="display: inline;">{% if options.banking %}{{ _("Bank Account") }}{% else %}{{ _("Score") }}{% end %}</h5></th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>{{ team.name }}</td>
                                {% if len(team.levels) > 0 %}
                                    <td style="text-align: center;">{{ team.levels[-1].name }}</td>
                                {% else %}
                                    <td style="text-align: center;">0</td>
                                {% end %}
                                {% if options.use_bots %}
                                    <td style="text-align: center;">{{ team.bot_count }}</td>
                                {% end %}
                                {% if hintcount > 0 %}
                                    <td style="text-align: center;">{{ len(team.hints) }}</td>
                                {% end %}
                                <td style="text-align: center;">{{ len(team.flags) }}</td>
                                <td style="text-align: right;">{% if options.banking %}${% end %}{{ team.money }}</td>
                            </tr>
                        </tbody>
                    </table>
                {% else %}
                    <strong>{{ _("You are not on a team") }}</strong>
                {% end %}
            </div>
        </div>

        {% if options.teams %}
        <div class="row">
            <div class="span11 offset1 well">
                <h2>
                    <i class="fa fa-flag"></i> 
                    {{ _("Player") }} {{ _("Mission Progress") }}
                </h2>
                <hr style="margin-bottom: 8px;">
                {% if game_started %}
                    {% for index, level in enumerate(GameLevel.all()) %}
                        {% set show_level = level.type != "hidden" or (level.type == "hidden" and level in team.game_levels) %}
                        {% if len(level.boxes) > 0 and team and show_level %}
                            {% if len(GameLevel.all()) > 1 %}
                            <h3 class="levelheader">
                                {% if level.type == 'none' or level in team.game_levels %}
                                    <i class="fa fa-unlock-alt"></i>
                                {% else %}
                                    <i class="fa fa-lock"></i>
                                {% end %}
                                {{ level.name }}&nbsp;
                            </h3>{% end %}
                            {% if level.description %}<h4 class="levelheader">({{ level.description }})<h4>{% end %}
                            {% if len(team.level_flags(level.number)) != 0 %}
                                <div class="progress" style="height: 8px; margin-bottom: 15px; background-color: #424242;">
                                    <div class="bar minibar" style="width: {{ ( len(team.level_flags(level.number)) /  float(len(level.flags)) ) * 100.0 }}%;" title="{{ len(team.level_flags(level.number)) }} / {{ len(level.flags) }}">
                                    </div>
                                </div>
                            {% end %}
                            {% if level.type == 'none' or level in team.game_levels %}
                                <hr style="margin: 5px 0;">
                                {% for box in level.boxes %}
                                    {% if not box.locked and len(box.flags) > 0 %}
                                    <div class="row" style="margin-left: 16px;">
                                        <div class="span4">
                                            <a href="/user/missions/boxes?uuid={{ box.uuid }}">
                                                <h5 style="display: inline;">
                                                    {% if box.operating_system.lower() == "windows" %}
                                                    <i class="fa fa-windows"></i>
                                                    {% elif box.operating_system.lower() == "linux" %}
                                                    <i class="fa fa-linux"></i>
                                                    {% elif box.operating_system.lower() == "apple" %}
                                                    <i class="fa fa-apple"></i>
                                                    {% elif box.operating_system.lower() == "?" %}
                                                    <i class="fa fa-desktop"></i>
                                                    {% else %}
                                                    <i class="fa fa-th"></i>
                                                    {% end %} {{ box.corporation.name }} - {{ box.name }}
                                                </h5>
                                            </a>
                                        </div>
                                        {% set flagcount=0 %} 
                                        {% for index, flag in enumerate(box.flags) %} 
                                            {% if flag in user.flags %} 
                                                {% set flagcount += 1 %}
                                            {% end %} 
                                        {% end %}
                                        <div class="span8" style="padding-top: 6px;">
                                            <table style="width: 100%; margin: 0 3px;"><tr>
                                            {% for index, flag in enumerate(box.flags) %} 
                                                <td style="padding: 0 3px;">
                                                    <div class="progress" style="height: 8px; background-color: #424242;" title="{{ flag.name }}">
                                                        <div class="bar minibar" style="width: {% if flag in user.flags %}100{% else %}0{% end %}%;"></div>
                                                    </div>
                                                </td>
                                            {% end %}
                                            </tr></table>
                                        </div>
                                    </div>
                                    {% end %}
                                {% end %}
                                <br/>
                            {% else %}
                                <hr style="margin: 5px 0;">
                            {% end %}
                        {% end %}
                    {% end %}
                {% else %}
                    <div class="row" style="padding-top: 15px;">
                        <div class="span1" style="text-align: right;">
                            <i class="fa fa-pause" style="font-size: 120%;"></i>
                        </div>
                        <div class="span7 msgdescription">
                                {{ _("Waiting for the game to start...") }}
                        </div>
                    </div>
                {% end %}
            </div>
        </div>
        {% end %} 

        {% if not visitor %}
        <div class="row">
            <div class="span11 offset1 well">
                <h2>
                    <i class="fa fa-trophy"></i>
                    {{ _("Game Information") }}
                </h2>
                <hr>
                <div class="row">
                    <div class="span1 offset1" style="text-align: center; padding-top: 5px;">
                        <i class="fa fa-sitemap" style="font-size: 180%;"></i>
                    </div>
                    <div class="span7 msgdescription">
                            {{ _("Welcome to the computer war game where security professionals can practice and learn.") }}  
                            {{ _("Each player will target challenges of varying difficulty and sophistication, attempting to collect flags and win glory.  Good luck!") }}
                    </div>
                </div>
                <br/>
                <div class="row">
                    <div class="span1 offset1" style="text-align: center; padding-top: 5px;">
                        <i class="fa fa-crosshairs" style="font-size: 180%;"></i>
                    </div>
                    <div class="span7 msgdescription">
                            {{ _("Rules of Engagement: Do not attack the game scoreboard platform.  Do not change the passwords of existing accounts in the network unless specified by a game objective.") }}
                            {{ _("Do not delete or modify community flags unless instructed. Do not attack other players unless outlined in the game goals.") }}
                    </div>
                </div>
                <br/>
                {% if options.dynamic_flag_value %}
                <div class="row">
                    <div class="span1 offset1" style="text-align: center; padding-top: 5px;">
                        <i class="fa fa-bar-chart-o" style="font-size: 180%"></i>
                    </div>
                    <div class="span7 msgdescription">
                        {% if options.dynamic_flag_type == "decay_future" %}
                            {{ _("This game is set up to use dynamic scoring based on speed, meaning as other players capture the flags, the future value of those flags will decrease.  So grab them fast or lose out!") }}
                        {% else %}
                            {{ _("This game is set up to use dynamic scoring based on difficulty, meaning as other players capture the flags, the overall value of those flags will decrease and potentially reduce your score.") }}
                        {% end %}
                    </div>
                </div>
                <br/>
                {% end %}
                {% if options.penalize_flag_value %}
                <div class="row">
                    <div class="span1 offset1" style="text-align: center; padding-top: 5px;">
                        <i class="fa fa-minus-circle" style="font-size: 180%;"></i>
                    </div>
                    <div class="span7 msgdescription">
                        {{ _("Failure has a cost. Be careful as penalties are applied when incorrect answers are submitted.") }}  
                        {{ _("The deduction is") }} {{ options.flag_penalty_cost }}% {{ _("of the flag's value. It will start on the") }} {% set n = options.flag_start_penalty %}{{str(n)+("th" if 4<=n%100<=20 else {1:"st",2:"nd",3:"rd"}.get(n%10, "th"))}} 
                        {{ _("attempt and continue until the") }} {% set n = options.flag_stop_penalty - 1 %}{{str(n)+("th" if 4<=n%100<=20 else {1:"st",2:"nd",3:"rd"}.get(n%10, "th"))}}. {{ _("No further penalty thereafter.") }}
                        <!-- Max loss is {{ (options.flag_stop_penalty - options.flag_start_penalty) * options.flag_penalty_cost }}% of the flag's value. -->
                    </div>
                </div>
                <br/>
                {% end %}
                {% if options.banking %}
                <div class="row">
                    <div class="span1 offset1" style="text-align: center; padding-top: 5px;">
                        <i class="fa fa-dollar" style="font-size: 180%;"></i>
                    </div>
                    <div class="span7 msgdescription">
                        {{ _("It's about the money and it's time to cash in on those skills!  This game is set up to use a banking system, so waste no time to build your fortune and buy what you need to win.") }}
                    </div>
                </div>
                <br/>
                {% end %}
                {% if options.use_black_market %}
                <div class="row">
                    <div class="span1 offset1" style="text-align: center; padding-top: 5px;">
                        <i class="fa fa-shopping-cart" style="font-size: 180%;"></i>
                    </div>
                    <div class="span7 msgdescription">
                        {{ _("The black market is open and you know a guy. Download a target's source code or S.W.A.T. other players by bribing the (in-game) police.") }}
                        {{ _("Player's bank account passwords are also available, allowing you to crack each other's passwords and steal the money.") }}
                    </div>
                </div>
                <br/>
                {% end %}
                {% if options.use_bots %}
                <div class="row">
                    <div class="span1 offset1" style="text-align: center; padding-top: 5px;">
                        <i class="fa fa-android" style="font-size: 180%;"></i>
                    </div>
                    <div class="span7 msgdescription">
                        {{ _("Time to create a botnet. By uploading a small bot program to target machines, you can be periodically rewarded for each bot in your botnet; the larger the botnet the larger the reward.") }}
                    </div>
                </div>
                <br/>
                {% end %}
            </div>
        </div>
        {% end %}
    </div>

    {% if options.teams and team and not visitor %}
    <div class="span5 well" style="width: 46%;">
        <h2>
            <i class="fa fa-group"></i>
            {{ _("Team Information") }}
        </h2>
        <hr>      
        <div class="row">
            <div class="span1 offset1" style="text-align: center; padding-top: 5px;">
                <i class="fa fa-key" style="font-size: 180%;"></i>
            </div>
            <div class="span7 msgdescription">
                    {{ team.code }}<br>{{ _("Share your team code to allow other players to join") }} {{ team.name }}.
            </div>
        </div>
    </div>
    {% end %}

    <div class="span5 well" style="width: 46%;">
        <h2>
            <i class="fa fa-flag"></i> 
            {% if options.teams %}{{ _("Team") }} {% end %}{{ _("Mission Progress") }}
        </h2>
        <hr style="margin-bottom: 8px;">
        {% if game_started %}
            {% for index, level in enumerate(GameLevel.all()) %}
                {% set show_level = level.type != "hidden" or (level.type == "hidden" and level in team.game_levels) %}
                {% if len(level.boxes) > 0 and team and show_level %}
                    {% if len(GameLevel.all()) > 1 %}
                    <h3 class="levelheader">
                        {% if level.type == 'none' or level in team.game_levels %}
                            <i class="fa fa-unlock-alt"></i>
                        {% else %}
                            <i class="fa fa-lock"></i>
                        {% end %}
                        {{ level.name }}&nbsp;
                    </h3>{% end %}
                    {% if level.description %}<h4 class="levelheader">({{ level.description }})<h4>{% end %}
                    {% if len(team.level_flags(level.number)) != 0 %}
                        <div class="progress" style="height: 8px; margin-bottom: 15px; background-color: #424242;">
                            <div class="bar minibar" style="width: {{ ( len(team.level_flags(level.number)) /  float(len(level.flags)) ) * 100.0 }}%;" title="{{ len(team.level_flags(level.number)) }} / {{ len(level.flags) }}">
                            </div>
                        </div>
                    {% end %}
                    {% if level.type == 'none' or level in team.game_levels %}
                        <hr style="margin: 5px 0;">
                        {% for box in level.boxes %}
                            {% if not box.locked and len(box.flags) > 0 %}
                            <div class="row" style="margin-left: 16px;">
                                <div class="span4">
                                    <a href="/user/missions/boxes?uuid={{ box.uuid }}">
                                        <h5 style="display: inline;">
                                            {% if box.operating_system.lower() == "windows" %}
                                            <i class="fa fa-windows"></i>
                                            {% elif box.operating_system.lower() == "linux" %}
                                            <i class="fa fa-linux"></i>
                                            {% elif box.operating_system.lower() == "apple" %}
                                            <i class="fa fa-apple"></i>
                                            {% elif box.operating_system.lower() == "?" %}
                                            <i class="fa fa-desktop"></i>
                                            {% else %}
                                            <i class="fa fa-th"></i>
                                            {% end %} {{ box.corporation.name }} - {{ box.name }}
                                        </h5>
                                    </a>
                                </div>
                                {% set flagcount=0 %} 
                                {% for index, flag in enumerate(box.flags) %} 
                                    {% if flag in team.flags %} 
                                        {% set flagcount += 1 %}
                                    {% end %} 
                                {% end %}
                                <div class="span8" style="padding-top: 6px;">
                                    <table style="width: 100%; margin: 0 3px;"><tr>
                                    {% for index, flag in enumerate(box.flags) %} 
                                        <td style="padding: 0 3px;">
                                            <div class="progress" style="height: 8px; background-color: #424242;" title="{{ flag.name }}">
                                                <div class="bar minibar" style="width: {% if flag in team.flags %}100{% else %}0{% end %}%;"></div>
                                            </div>
                                        </td>
                                    {% end %}
                                    </tr></table>
                                </div>
                            </div>
                            {% end %}
                        {% end %}
                        <br/>
                    {% else %}
                        <hr style="margin: 5px 0;">
                    {% end %}
                {% end %}
            {% end %}
        {% else %}
            <div class="row" style="padding-top: 15px;">
                <div class="span1" style="text-align: right;">
                    <i class="fa fa-pause" style="font-size: 120%;"></i>
                </div>
                <div class="span7 msgdescription">
                        {{ _("Waiting for the game to start...") }}
                </div>
            </div>
        {% end %}
    </div>


    {% if len(Category.all()) > 2 %}
    <!-- Only looks good if there are at least 3 categories -->
    <div class="span5 well" style="width: 46%;">
        <h2><i class="fa fa-star"></i> {% if options.teams %}{{ _("Team") }} {% end %}{{ _("Skill Progress") }}</h2>
        <hr style="margin-bottom: 8px;">
        {% if game_started %}
            <div id="spider_skills" style="height:400px; max-width: 550px;" data-uuid="{{ team.uuid }}" data-name="{{ _('Categories') }}">
                <!-- Spider Chart Here -->
            </div>
        {% else %}
            <div class="row" style="padding-top: 15px;">
                <div class="span1" style="text-align: right;">
                    <i class="fa fa-pause" style="font-size: 120%;"></i>
                </div>
                <div class="span7 msgdescription">
                        {{ _("Waiting for the game to start...") }}
                </div>
            </div>
        {% end %}
    </div>
    {% end %} 
</div>
{% end %}
